/**
 * 管理机
 */
import "reflect-metadata";
/**
 * 引入公共文件
 */
import "../common";
/**
 * 管理机的主要职责是 管理本脚本下的所有任务
 * 1. 管理UI任务
 * 2. 管理脚本任务
 * 3. 务必轻量
 */
import { container as injector } from 'tsyringe'
import { Timer } from "../autox/timer";
import { Task } from "../autox/task";
import { kwai, $ } from "../utils";
import { sendToWebsocket } from "../utils/send";

const timer = injector.resolve(Timer)
const task = injector.resolve(Task)

timer.setInterval(() => { }, 1000 * 10)

events.on('exit', () => {
    sendToWebsocket({ type: 'FIND_RED_PACKET_TASK_FINISH', payload: { pkg: $.PACKAGES["com.smile.gifmaker"] } })
    injector.dispose()
})

let count: number = 0;
try {
    toastLog(`任务开始执行`)
    task.start(() => {
        // kwai.openAndJoinLiveRoom();
        while (true) {
            count += 1;
            try {
                const text = kwai.getLiveRoomText()
                console.log(text)
                const redInfo = kwai.getLiveRoomRedPacketInfo()
                console.log({ redInfo })
                sendToWebsocket({
                    type: 'FIND_RED_PACKET',
                    payload: redInfo
                });
                toastLog(`直播间扫描结束，总共扫描了${count}个房间`)
                sleep(1000 * 3)
            } catch (e) {
                toastLog(e.message)
            } finally {
                $.swipeUp()
            }
        }
    });
    // 检查意外跳转的弹框
    task.start(() => {
        while (true) {
            const texts = kwai.getLiveRoomText()
            const text = texts.join(' ')
            if (text.includes('我知道了')) {
                back()
            }
            if (text.includes('直播结束')) {
                $.swipeUp()
            }
            sleep(1000)
        }
    });
} catch (e) {
    toastLog(e.message)
}
